package com.luyun.frogpay.view.facepay;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.luyun.frogpay.R;
import com.luyun.frogpay.base.baseclass.ActivityManager;
import com.luyun.frogpay.base.baseclass.BaseActivity;
import com.luyun.frogpay.base.routepath.RouteActivityPath;
import com.luyun.frogpay.pojo.KqwSpeechCompound;
import com.luyun.frogpay.view.home.HomeActivity;

import butterknife.BindView;
import butterknife.OnClick;
import lombok.val;

@Route(path = RouteActivityPath.FACE_PAY_RESULT_ACTIVITY)
public class FacePayResultActivity extends BaseActivity<FacePayResultPresenter> implements FacePayResultContract.IFacePayResultView {

    /**
     * @see HomeActivity#facePayResult(boolean)
     */
    @Autowired(name = "isFacePaySuccess")
    boolean isFacePaySuccess;

    @BindView(R.id.iv_pay_result)
    ImageView ivPayResult;

    @BindView(R.id.tv_pay_result)
    TextView tvPayResult;

    @BindView(R.id.totals)
    TextView totals;
    @BindView(R.id.btn_back_home)
    Button btnBackHome;

    @Autowired
     public double Totals;

    private CountDownTimer countDownTimer;
    private KqwSpeechCompound kqwSpeechCompound;

    @Override
    protected void initView() {
        ActivityManager.getInstance().addActivity(this);
        ARouter.getInstance().inject(this);
        kqwSpeechCompound=new KqwSpeechCompound(context);



    }

    @Override
    protected void onStart() {
        super.onStart();
        tvPayResult.setText(isFacePaySuccess ? "支付成功" : "支付失败");
        btnBackHome.setBackgroundResource(isFacePaySuccess ? R.drawable.shape_btn_pay_success : R.drawable.shape_btn_pay_failed);
        btnBackHome.setTextColor(Color.parseColor(isFacePaySuccess ? "#ff54b273" : "#ff999999"));
        ivPayResult.setImageResource(isFacePaySuccess ? R.drawable.icon_pay_success : R.drawable.icon_pay_failed);
        totals.setText(isFacePaySuccess ? "￥"+Totals : "");
        if (isFacePaySuccess) {
            kqwSpeechCompound.speaking("支付成功" + Totals + "元");
        } else {
            kqwSpeechCompound.speaking("支付失败");
        }
        countDown();
    }

    @Override
    protected void initInjector() {
        activityComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_face_pay_result;
    }

    @OnClick(R.id.btn_back_home)
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_back_home:
                countDownTimer.cancel();
                finish();
                break;
        }
    }

    /**
     * 倒计时显示
     */
    private void countDown() {

        countDownTimer = new CountDownTimer(4000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                if (millisUntilFinished == 0){
                    btnBackHome.setText("回到首页(0s)");
                }else {
                    btnBackHome.setText("回到首页(" + millisUntilFinished / 1000 + "s)");
                }
            }

            @Override
            public void onFinish() {
                finish();
            }
        }.start();
    }
}
